package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    OrdersService ordersService;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    AddressBookMapper addressBookMapper;

    /*
     *   去支付，生成订单
     * */
    @PostMapping("/submit")
    public R<String> submitOrder(@RequestBody Orders orders) {
        return ordersService.submit(orders);
    }


    /*
     *   移动端分页查询订单
     * */

    @GetMapping("/userPage")
    public R page(Integer page, Integer pageSize) {
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        //分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, userId);
        ordersService.page(ordersPage, lqw);
        BeanUtils.copyProperties(ordersPage, ordersDtoPage);
        //获取订单
        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> ordersDtos = records.stream().map((order) -> {
            Long orderId = order.getId();
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(qw);
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());
/*
            ArrayList<OrdersDto> ordersDtos = new ArrayList<>();
        for (OrdersDto record : records) {
            Long recordId = record.getId();
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(OrderDetail::getOrderId,recordId);
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(qw);
            record.setOrderDetails(orderDetails);
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            ordersDtos.add(ordersDto);
        }*/
        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }


    /*
     *  后台分页查询订单
     * */
    @GetMapping("/page")
    public R ordersDtoPage(Integer page, Integer pageSize
            , Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        // 获取用户id
        //Long userId = BaseContext.getCurrentId();
        //获取订单号
        //获取订单开始时间和结束时间
        //分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        lqw.eq(number != null, Orders::getNumber, number);

        if (beginTime != null && endTime != null) {
            lqw.between(Orders::getOrderTime, beginTime, endTime);
        }
        ordersService.page(ordersPage, lqw);
        //获取订单集合
        List<Orders> records = ordersPage.getRecords();
        for (Orders order : records) {
            Long addressBookId = order.getAddressBookId();
            AddressBook addressBook = addressBookMapper.selectById(addressBookId);
            if (addressBook != null) {
                order.setUserName(addressBook.getConsignee());
            }
        }
        ordersPage.setRecords(records);
        return R.success(ordersPage);
    }
}
